<?php

/**

 * Created by PhpStorm.

 * User: Administrator

 * Date: 2018/9/18

 * Time: 14:31

 */

namespace app\index\controller;

use app\index\model\Tyre;

use function GuzzleHttp\Psr7\str;

use think\Build;

use think\Request;

use app\index\model\Member as MemberModel;

use think\Exception;

use think\Cache;

use think\Loader;

use think\Db;

use think\View;

use app\common\PlsDemo;

use think\Url;

class Member

{

    /**

     * 会员登录

     * @return array

     * @throws Exception

     */

    public function login()

    {

        $username = Request::instance()->param('username');

        $password =Request::instance()->param('password');

        if (empty($username)) throw new Exception('username not Empty');

        if (empty($password)) throw new Exception('password not Empty');

        $model = new MemberModel();

        // 验证数据



        $user_id = $model->where(['username' => $username,'password' => (sha1($password))])->value('id');

        $member = $model->where('username',$username)->find();

        if(empty($member)){

            return ['code' => 401, 'message' => '该用户不存在'];

        }

        if(empty($member['wechat_id'])) $member['wechat_id'] = '';

        if ($user_id) {

            return ['code' => 200, 'message' => '登录成功', 'data' => ['user_id' => $user_id],'user_id' => $user_id,'unionid' => $member['wechat_id'],'openid' => ''];

        }

        return ['code' => 401, 'message' => '账号或密码错误'];



    }
	public function login_staff()

    {

        //if (!Request::instance()->isPost()) return ['code' => 400, 'message' => '请求方式错误'];



        // 获取参数

        try {

            $username = Request::instance()->param('username');

            //$password = mcs(Request::instance()->param('password'));
			$password = Request::instance()->param('password');
			$shop_id = Request::instance()->param('shop_id');

            if (empty($username)) throw new Exception('username not Empty');

            if (empty($password)) throw new Exception('password not Empty');
			//if (empty($shop_id)) throw new Exception('shop_id not Empty');


            $model = new MemberModel();
            // 验证数据
			$user_id = Db::name('shop_assignor')->where(['telephone' => $username, 'password' => $password,'is_delete' => 1])->value('id');
			//是否删除
			$is_delete = Db::name('shop_assignor')->where(['telephone' => $username, 'password' => $password,'is_delete' => 1])->value('is_delete');
			$shop_id_a = Db::name('shop_assignor')->where(['telephone' => $username, 'password' => $password,'is_delete' => 1])->value('shop_id');
			
            //$user_id = $model->where(['username' => $username, 'password' => $password,'status' => 1])->value('id');
			


            if ($user_id) {
				if($is_delete=='2'){
					 return ['code' => 401, 'message' => '登录失败,该会员已被删除'];	
				}
                return ['code' => 200, 'message' => '登录成功', 'data' => ['user_id' => $user_id,'shop_id' => $shop_id_a]];
            }
            //return ['code' => 401, 'message' => '账号或密码错误' ,'password'=>];
			return ['code' => 401, 'message' => '账号或密码错误'];
        } catch (Exception $e) {

            return ['code' => $e->getCode(), 'message' => $e->getMessage()];

        }



    }
	//////////////////////////////////////////////////////////////////////////



    public function t()

    {

        $day = '2147483647';

        $data = date('Y-m-d H:i:s', time() + 3600 * 24 * $day);

        halt($data);

    }



    public function get_json()

    {

        $param = Request::instance()->param();

        halt($param);

        //halt(file_get_contents('php://input')); //application/x-www.form-urlencoded和请求包

        halt($GLOBALS['HTTP_RAW_POST_DATA']);

    }

    /**

     * 会员邀请码

     */

    public function qrcode($member_id)

    {

        $url = Request::instance()->domain()."/index.php/mobile/user_port/login?uid=".$member_id;

        Loader::import('phpqrcode.phpqrcode', EXTEND_PATH);

        $object = new \QRcode();

        $path = ROOT_PATH . 'public/index/member/' . $member_id . '.png';

        //第二个参数false的意思是不生成图片文件，如果你写上‘picture.png’则会在根目录下生成一个png格式的图片文件

        $data = Db::name('member_distribution md')

            ->where('hierarchy','like','%,'.$member_id.',%')

            ->field('m.username,md.create_time')

            ->join('member m','md.referrals_id = m.id')

            ->select()->toArray();

        foreach ($data as $k => &$v){

            //$v['username'] = hideStr($v['username'],3,4);

            $v['username'] = hideStr($v['username'],3,4);

        }

        $ret = $object->png($url, $path, 10, 10, 2);

        $content = Db::name('about')->where('id',7)->value('content');

        view::share('content',strip_tags($content));





        $salesman_id = Db::name('member')->where('id',$member_id)->value('salesman_id');

        if(empty($salesman_id)) $salesman_id = 1;





        //

//        if($data['salesman_id'] > 0){

//            $manager = Db::name('manager')->where('id',$data['salesman_id'])->find();

//            $data['ywy_name'] = $manager['nickname'];

//            $data['ywy_tel'] = $manager['telephone'];

//            $data['erweima'] = "http://lihai.xmdonkey.com/salesman/{$data['salesman_id']}.png";

//        }

        return view('',['member_id' => $member_id,'data' => $data,'salesman_id' => $salesman_id]);

    }



    public function choose_system()

    {

        $view = new View();

        echo $view->fetch('');

    }





    /**

     * 会员注册

     */

    public function register()

    {

        Db::startTrans();

        try {

            $param = Request::instance()->param();



            $validate = Loader::validate('Member');



            if (!$validate->check($param)) return ['code' => 400, 'message' => $validate->getError()];



            $model = new MemberModel();

            $model->create_time = date('Y-m-d H:i:s', time());

            $model->password = sha1($param['password']);

            $model->username = $param['username'];

//            $results = $model->validate($param,'member');

//            if($results !== true){

//                return ['code' => 400,'message' => $results->getError()];

//            }



            $result = $model->allowField(true)->save();



            Db::commit();

            if ($result !== false) {

                (new Coupon())->send(3, $model->id);

                Db::name('member_distribution')->insert(['distribution_id' => '0', 'referrals_id' => $model->id, 'hierarchy' => '0,', 'level' => '1', 'create_time' => date('Y-m-d H:i:s', time())]);



                return ['code' => 200, 'message' => '注册成功', 'data' => ['user_id' => $model->id]];

            }

            return ['code' => 400, 'message' => '登录失败'];

        } catch (\Exception $e) {

            Db::rollback();

            return ['code' => 500, 'm' => $e->getMessage(), 'l' => $e->getLine()];

        }

    }









    public function register2(){

        Db::startTrans();

        try {

            $param = Request::instance()->param();

            $smsVerify = Cache::get('sms_verify_reg_' . $param['username']);



            $validate = Loader::validate('Member');



            if (!$validate->check($param)) return ['code' => 400, 'message' => $validate->getError()];

            if ($smsVerify != $param['code']) {

                return ['code' => 401, 'message' => '验证码错误'];

            }

            $model = new MemberModel();

            $model->create_time = date('Y-m-d H:i:s', time());

            $model->password = sha1($param['password']);

            $model->username = $param['username'];

//            $results = $model->validate($param,'member');

//            if($results !== true){

//                return ['code' => 400,'message' => $results->getError()];

//            }



            $result = $model->allowField(true)->save();



            Db::commit();

            if ($result !== false) {

                Db::name('member_distribution')->insert(['distribution_id' => '0', 'referrals_id' => $model->id, 'hierarchy' => '0,', 'level' => '1', 'create_time' => date('Y-m-d H:i:s', time())]);

                return ['code' => 200, 'message' => '注册成功', 'data' => ['user_id' => $model->id]];

            }

            return ['code' => 400, 'message' => '登录失败'];

        } catch (\Exception $e) {

            Db::rollback();

            return ['code' => 500,'m' =>$e->getMessage(),'l' => $e->getLine()];

        }

    }



    /**

     * 短信验证码

     * @return array

     */

    public function get_code()

    {



        //if (!Request::instance()->isPost()) return ['code' => 400, 'message' => '请求方式错误'];



        $phone = Request::instance()->param('phone');

        $type = Request::instance()->param('type');

        $user_id = MemberModel::where(['username' => $phone])->value('id');



        if ($type == 'reg' && !empty($user_id)) {

            $find = Db::name('member')->where('username',$phone)->find();

            if(empty($find))  return ['code' => 402, 'message' => '用户未注册'];

            // 注册获取验证码

            return ['code' => 402, 'message' => '用户已注册'];



        } elseif ($type == 'find' && empty($user_id)) {

            $find = Db::name('member')->where('username',$phone)->find();

            if(empty($find))  return ['code' => 402, 'message' => '用户未注册'];

            // 找回密码获取验证码

            return ['code' => 402, 'message' => '用户未注册'];



        } elseif ($type == 'update' && empty($user_id)) {

            $find = Db::name('member')->where('username',$phone)->find();

            if(empty($find))  return ['code' => 402, 'message' => '用户未注册'];

            // 修改密码

            return ['code' => 402, 'message' => '用户未注册'];

        }elseif($type == 'forget'){

            $find = Db::name('member')->where('username',$phone)->find();

            if(empty($find))  return ['code' => 402, 'message' => '用户未注册'];

        }elseif($type == 'bind_tel'){ //验证手机号



        }



        $smsVerify = Cache::get('sms_verify_' . $type . '_time_' . $phone);



        if (!empty($smsVerify)) {

           // return ['code' => 401, 'message' => '60s内只能发送一次'];

        }



        $code = rand(100000, 999999);



        $content = '您好,您的短信验证码为：' . $code;

        $result = sendSMS($phone, $code);



        if ($result) {

            Cache::set('sms_verify_' . $type . '_time_' . $phone, $code, 60); // 短信请求有效期

            Cache::set('sms_verify_' . $type . '_' . $phone, $code, 3600);     // 短信保存有效期

            return ['code' => 200, 'message' => '发送成功', 'data' => ['code' => $code, 'phone' => $phone]];

        }

    }





    public function my($member_id)

    {

        try {

            Cache::set('member_id' . $member_id, time());

            $data = Db::name('member')->where('id', $member_id)->find();

            $data['ywy_name'] = '';

            $data['ywy_tel'] = '';//Db::name('manager')->where('id',1)->value('telephone');

            $data['erweima'] = 'http://lihai.xmdonkey.com/salesman/1.png';

            //

            $data['erweima'] = 'http://lihai.xmdonkey.com/index.php/index/member/qrcode/member_id/' . $member_id;

            $data['salesman_id'] = Db::name('share_member')

                ->where('member_id', $member_id)->value('manage_id');



            if (is_null($data['salesman_id'])) $data['salesman_id'] = 0;

            if ($data['salesman_id'] > 0) {

                $manager = Db::name('manager')->where('id', $data['salesman_id'])->find();

                $data['ywy_name'] = $manager['nickname'];

                $data['ywy_tel'] = $manager['telephone'];

                ///$data['erweima'] = "http://lihai.xmdonkey.com/salesman/{$data['salesman_id']}.png";

                $data['erweima'] = 'http://lihai.xmdonkey.com/index.php/index/member/qrcode/member_id/' . $member_id;

            }

            if (is_null($data['ywy_name']) || is_null($data['ywy_tel'])) {

                $data['ywy_name'] = '';

                $data['ywy_tel'] = '';

            }

            if ($data['salesman_id'] == 1) {

                $data['ywy_name'] = '';

                $data['ywy_tel'] = '';

            }

            $order_no_pay_23 = Db::name('order')->where('member_id', $member_id)->where('is_delete', 1)->where('status', 1)->where('order_type', 'in', '2,3')->count();//未支付 代付款

            $order_no_pay_1 = Db::name('order')->where('member_id', $member_id)->where('is_delete', 1)->where('status', 'in', '1')->count();//未支付 代付款

            $order_no_pay = $order_no_pay_1;

            $order_pay = Db::name('order')->where('member_id', $member_id)->where('is_delete', 1)->where('status', 'in', '2,3')->count();//待收货

            $order_evaluate = Db::name('order')->where('member_id', $member_id)->where('is_delete', 1)->where('status', 5)->count();//带评价

            $order_refund = Db::name('order')->where('member_id', $member_id)->where('is_delete', 1)->where('status', 'in', '10')->count();//退款中

            $order6 = Db::name('order')->where('member_id', $member_id)->where('is_delete', 1)->where('status', 6)->count();//进行中 礼包6和服务6

            $versions = (int)Db::name('Config')->where('varname', 'web_android_version')->value('varvalue');

            return ['code' => 200, 'message' => '会员中心', 'data' => $data, 'order_no_pay' => $order_no_pay, 'order_pay' => $order_pay, 'order_evaluate' => $order_evaluate, 'order_refund' => $order_refund, 'order_service_6' => $order6, 'versions' => $versions];

        } catch (\Exception $e) {

            return ['code' => 400, 'message' => '网络异常'];

        }

    }







    /**

     * 单一字段值设置

     * @param $member_id

     * @param $field

     * @param $value

     * @return array

     */

    public function setfield($member_id,$field,$value)

    {

        $ret = Db::name('member')->where('id', $member_id)->setField($field, $value);

        if ($ret != false) {

            return ['code' => 200, 'message' => '设置成功'];

        }

        return ['code' => 400, 'message' => '名称设置'];

    }



    protected function date($variable)

    {

        $year  = mb_substr($variable, 0, 4);

        $month = mb_substr($variable, 5, 2);

        $day   = mb_substr($variable, 8, 2);



        $date_create = date_create($year . '-' . $month . '-' . $day);



        $date = date_format($date_create, 'Y-m-d H:i:s');

        return $date;

    }



    /**实名认证

     * @param $member_id

     * @param $real_name

     * @param $id_number

     * @return array

     */

    public function certification($member_id,$real_name,$id_number='',$phone, $car_number, $car_model, $id_code, $test_time = '', $destory_time = '', $car_name = '', $engine_number, $brand_number, $register_time = '',$car_business_time = '')

    {

         $check_is_show=Db::name('config')->where('varname','in','web_is_show')->value('varvalue');

         if ($check_is_show=='N'){

            if (empty($id_number)||empty($destory_time)||empty($car_business_time)){



                return ['code' => 400,'message' => '字段不能为空'];

            }

         }

         //car_business_time

         //destory_time

         //register_time

         //test_time

        $is_certification = Db::name('member')->where('id',$member_id)->value('is_certification');

        $sql_car_number = Db::name('member')->column('car_number');

        if(in_array($car_number,$sql_car_number)){

            return ['code' => 400,'message' => '该车牌已经实名认证过了,请更换车牌'];

        }



        if($is_certification == 1){



        }else{

            return ['code' => 400,'message' => '认证中'];

        }

        $cer = Cache::get('member_id'.$member_id);

        if(empty($cer)){

            //return ['code' => 400,'message' => '请不要重复提交'];

        }else{

            Cache::rm('member_id'.$member_id);

        }

        if(strstr($car_business_time, '年') !== false || strstr($car_business_time, '-')){



        }

        //return $this->date($car_business_time);

        if ($check_is_show=='N'){

            writeLogPic('init:');

            writeLogPic('--------------start---member_id:'.$member_id.' AppType:'.getAppType().' 参数:'.$_SERVER['HTTP_USER_AGENT']);

            $file1 = request()->file('id_face');

            $file2 = request()->file('id_back');

            $file3 = request()->file('car_face');

            $file4 = request()->file('car_back');



            writeLogPic('|||'.$file1.'-'.$file2.'-'.$file3.'-'.$file4.'|||');

            $id_face = uploadImg('id_face');

            $id_back = uploadImg('id_back');

            $car_face = uploadOne('car_face');

            $car_back = uploadOne('car_back');

            writeLogPic('---------------end------------------------------');

        }else{

            writeLogPic('init:');

            writeLogPic('--------------start---member_id:'.$member_id.' AppType:'.getAppType().' 参数:'.$_SERVER['HTTP_USER_AGENT']);

//            $file1 = request()->file('id_face');

//            $file2 = request()->file('id_back');

            $file3 = request()->file('car_face');

            $file4 = request()->file('car_back');



            writeLogPic('|||'.$file3.'-'.$file4.'|||');

//            $id_face = uploadImg('id_face');

//            $id_back = uploadImg('id_back');

            $id_face=$id_back='';

            $car_face = uploadOne('car_face');

            $car_back = uploadOne('car_back');

            writeLogPic('---------------end------------------------------');

        }



        if(empty($member_id) || empty($real_name)){

            return ['code' => 400,'message' => '字段不能为空'];

        }



        $ret = Db::name('member')->where('id',$member_id)->update([

            'real_name' => $real_name,

            'id_number' => $id_number,

            'id_face' => $id_face,

            'id_back' => $id_back,

            'is_certification' => 3,

            'phone' => $phone,

            'car_name' => $real_name,

            'engine_number' => $engine_number,

            'brand_number' => $brand_number,

            'register_time' => $register_time ? $this->date($register_time) : '',

            'car_number' => $car_number,

            'car_model' => $car_model,

            'car_face' => $car_face,

            'car_back' => $car_back,

            'id_code' => $id_code,

            'test_time' => $test_time ? $this->date($test_time) : '',

            'car_business_time' => $car_business_time ? $this->date($car_business_time) : '',

            'destory_time' =>  $destory_time ? $this->date($destory_time) : '',

            'car_create_time' => date('Y-m-d H:i:s'),

        ]);



        if($ret){

          return ['code' => 200,'message' => '资料提交成功,等待审核'];

        }

        return ['code' => 400,'message' => '失败'];

    }



    public function certification2($member_id,$real_name,$id_number,$phone, $car_number, $car_model, $id_code, $test_time, $destory_time, $car_name = '', $engine_number, $brand_number, $register_time,$car_business_time = '')

    {

        //car_business_time

        //destory_time

        //register_time

        //test_time

        if(strstr($car_business_time, '年') !== false || strstr($car_business_time, '-')){



        }

        //return $this->date($car_business_time);

        writeLogPic('--------------start---member_id:'.$member_id.' AppType:'.getAppType().' 参数:'.$_SERVER['HTTP_USER_AGENT'].':getOS:'.getOS());

        $id_face = uploadImg('id_face');

        $id_back = uploadImg('id_back');

        $car_face = uploadOne('car_face');

        $car_back = uploadOne('car_back');

        writeLogPic('---------------end------------------------------');

        if(empty($member_id) || empty($real_name) || empty($id_number)){

            return ['code' => 400,'message' => '字段不能为空'];

        }

        $ret = Db::name('member')->where('id',$member_id)->update([

            'real_name' => $real_name,

            'id_number' => $id_number,

            'id_face' => $id_face,

            'id_back' => $id_back,

            'is_certification' => 3,

            'phone' => $phone,

            'car_name' => $real_name,

            'engine_number' => $engine_number,

            'brand_number' => $brand_number,

            'register_time' => $this->date($register_time),

            'car_number' => $car_number,

            'car_model' => $car_model,

            'car_face' => $car_face,

            'car_back' => $car_back,

            'id_code' => $id_code,

            'test_time' => $this->date($test_time),

            'car_business_time' => $this->date($car_business_time),

            'destory_time' => $this->date($destory_time),

            'car_create_time' => date('Y-m-d H:i:s'),

        ]);

        if($ret){

            return ['code' => 200,'message' => '资料提交成功,等待审核'];

        }

        return ['code' => 400,'message' => '失败'];

    }





    /**头像修改

     * @param $member_id

     * @return array

     */

    public function head_pic($member_id)

    {

        $head_pic = uploadImg('head_pic');

        if (empty($head_pic)) return ['code' => 400, 'message' => '图片失败了'];

        $ret = Db::name('member')->where('id', $member_id)->setField(['avatar' => $head_pic]);

        if ($ret) {

            return ['code' => 200, 'message' => '头像上传成功', 'head_pic' => $head_pic];

        }

        return ['code' => 400, 'message' => '失败'];

    }



    /**

     * 忘记密码/修改密码

     * @param $username

     * @param $code

     * @param $password

     * @param $confirm_password

     * @return array

     */

    public function update_password($username, $code, $password, $confirm_password)

    {

        $sql_user = Db::name('member')->where('username', $username)->find();

        if (empty($sql_user)) return ['code' => 400, 'message' => '此用户不存在'];

        $cache_code = Cache::get('sms_verify_' . 'update' . '_' . $username);     // 短信保存有效期

        if ($cache_code != $code) {

            return ['code' => 400, 'message' => '验证码不正确'];

        }

        if ($password != $confirm_password) {

            return ['code' => 400, 'message' => '两次密码不一致'];

        }

        $ret = Db::name('member')->where('username|id', $username)->setField('password', sha1($password));

        if ($ret) {

            return ['code' => 200, 'message' => '密码修改成功'];

        }

        return ['code' => 400, 'message' => '失败'];

    }



    /**

     * @param $order_id

     * @return array

     * 确认取走轮胎

     */

    public function take_tyre($order_id,$member_id)

    {

        $status = Db::name('order')->where('id', $order_id)->where('member_id',$member_id)->value('status');

        if($status == 1){

            return ['code' => 400, 'message' => '此订单还没有支付'];

        }

        $ret = Db::name('order')->where('take_tyre',4)->where('id', $order_id)->where('member_id',$member_id)->update(['take_tyre' =>2,'take_tyre_time' => date('Y-m-d H:i:s')]);

        if($ret){

            return ['code' => 200, 'message' => '轮胎已确认取走'];

        }

        $ret2 = Db::name('order')->where('take_tyre',3)->where('id', $order_id)->where('member_id',$member_id)->setField('take_tyre',1);

        if($ret2){

            return ['code' => 200, 'message' => '轮胎已确认寄存'];

        }

        return ['code' => 400, 'message' => '失败'];

    }



    /**

     *

     * 保单

     */

    public function protect_order()

    {

        $param = Request::instance()->param();

        $ret = Db::name('protect_order')->where('member_id', $param['member_id'])->insert();

    }



    public function take(){

        if(strpos('5,4',5) !== false){

            echo 'hah';

        }

    }



    /**

     * @param $manage_io

     * 业务员邀请id

     */





    /**

     * 轮胎寄存

     */

    public function tyre($member_id){

        //$username = Db::name('member')->where('id',$member_id)->value('username');

        $data = (new \app\console\model\Order())->where('member_id',$member_id)

            ->where('status','in','6,5,4')

            ->where('order_type',2)

//            ->where('faqi',2)

            ->where('take_tyre','in',[3,1,4,2])

            ->where('single_id',11)

            ->order('id','desc')

            ->field('id,amount price,order_number,start_time,end_time,multi_pic,explain,take_tyre,take_tyre_time')

            ->paginate(6)->each(function($item,$key){

            $item['multi_pic'] = unserialize($item['multi_pic']) ? unserialize($item['multi_pic']) : [];

            $item['remark'] = $item['explain'];

        });



        return ['code' => 200, 'message' => '轮胎寄存列表','data' => $data];

//        // 获取表单上传文件

//        $multi_pic = [];

//        $files = request()->file('image');

//        foreach($files as $file){

//            // 移动到框架应用根目录/public/uploads/ 目录下

//            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');

//            if($info){

//            $multi_pic[] = $info->getFilename();

//                // 输出 42a79759f284b767dfcb2a0197904287.jpg

//            }else{

//                // 上传失败获取错误信息

//                return $file->getError();

//            }

//        }

//        $insert = ['member_id' => $member_id,'number' => $number,'remark' => $remark,'start_time' => $start_time,'end_time' => $end_time,'multi_pic' => json_encode($multi_pic)];

//

//        $ret = Db::name('tyre')->insertGetId($insert);

//        if($ret){

//            return ['code' => 200, 'message' => '提交成功','multi_pic' => $multi_pic];

//        }

//        return ['code' => 400, 'message' => '失败'];



    }

    public function tyre1($member_id){



        // 获取表单上传文件

        $multi_pic = [];

        $files = request()->file('image');

        foreach($files as $file){

            // 移动到框架应用根目录/public/uploads/ 目录下

            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');

            if($info){

            $multi_pic[] = $info->getFilename();

                // 输出 42a79759f284b767dfcb2a0197904287.jpg

            }else{

                // 上传失败获取错误信息

                return $file->getError();

            }

        }

        $insert = ['member_id' => $member_id,'number' => 1,'remark' => 2,'start_time' => 3,'end_time' => 4,'multi_pic' => serialize($multi_pic)];



        $ret = Db::name('tyre')->insertGetId($insert);

        if($ret){

            return ['code' => 200, 'message' => '提交成功','multi_pic' => $multi_pic];

        }

        return ['code' => 400, 'message' => '失败'];



    }



    /**

     * @param $member_id

     * @param $car_number

     * @param $car_model

     * @return array

     * 我的车库添加

     */

    public function my_car_create($id_number,$car_business_time,$phone, $member_id, $car_number, $car_model, $id_code, $test_time, $destory_time, $car_name, $engine_number, $brand_number, $register_time)

    {



        $session_get = \think\Cache::get('car_'.$member_id);

        if(!$session_get){

            return ['code' => 400, 'message' => '已经提交过了'];

        }

        \think\Cache::rm('car_'.$member_id);

        $car_face = uploadOne('car_face');

        $car_back = uploadOne('car_back');

        $id_face = uploadOne('id_face');

        $id_back = uploadOne('id_back');

        $username = Db::name('member')->where('id',$member_id)->value('username');

        $insert = [

            'username' => $username,

            'phone' => $phone,

            'car_name' => $car_name,

            'engine_number' => $engine_number,

            'brand_number' => $brand_number,

            'register_time' => $this->date($register_time),

            'member_id' => $member_id,

            'car_number' => $car_number,

            'car_model' => $car_model,

            'car_face' => $car_face,

            'car_back' => $car_back,

            'id_face' => $id_face,

            'id_back' => $id_back,

            'id_code' => $id_code,

            'id_number' => $id_number,

            'car_business_time' => $this->date($register_time),

            'test_time' => $this->date($test_time),

            'destory_time' => $this->date($destory_time),

            'create_time' => date('Y-m-d H:i:s'),

        ];

        $ret = Db::name('car')->insert($insert);

        if ($ret) {

            return ['code' => 200, 'message' => '添加成功', 'car_face' => $car_face, 'car_back' => $car_back, 'id_face' => $id_face, 'id_back' => $id_back];

        }

        return ['code' => 400, 'message' => '失败'];



    }





    public function my_car_update($id_number, $car_bussiness_time, $phone, $id, $member_id, $car_number, $car_model, $id_code, $test_time, $destory_time, $car_name, $engine_number, $brand_number, $register_time)

    {



        $car_face = uploadOne('car_face');

        $car_back = uploadOne('car_back');

        $id_face = uploadOne('id_face');

        $id_back = uploadOne('id_back');

        $insert = [

            'phone' => $phone,

            'car_name' => $car_name,

            'engine_number' => $engine_number,

            'brand_number' => $brand_number,

            'register_time' => $this->date($register_time),

            'id' => $id,

            'member_id' => $member_id,

            'car_number' => $car_number,

            'car_model' => $car_model,

            'car_face' => $car_face,

            'car_back' => $car_back,

            'id_face' => $id_face,

            'id_back' => $id_back,

            'id_number' => $id_number,

            'car_business_time' => $this->date($car_bussiness_time),

            'id_code' => $id_code,

            'test_time' => $this->date($test_time),

            'destory_time' => $this->date($destory_time),

        ];

        $ret = Db::name('car')->update($insert);

        if ($ret !== false) {

            return ['code' => 200, 'message' => '修改成功', 'car_face' => $car_face, 'car_back' => $car_back];

        }

        return ['code' => 400, 'message' => '失败'];



    }



    public function my_car_update1($id_number, $car_business_time, $phone, $id, $member_id, $car_number, $car_model, $id_code, $test_time, $destory_time, $car_name, $engine_number, $brand_number, $register_time)

    {

        $car_face_url = uploadOne1('car_face');

        $car_back_url = uploadOne1('car_back');

        $id_face_url = uploadOne1('id_face');

        $id_back_url = uploadOne1('id_back');

        $insert = [

            'phone' => $phone,

            'car_name' => $car_name,

            'engine_number' => $engine_number,

            'brand_number' => $brand_number,

            'register_time' => $this->date($register_time),

            'id' => $id,

            'member_id' => $member_id,

            'car_number' => $car_number,

            'car_model' => $car_model,

            'car_face' => $car_face_url,

            'car_back' => $car_back_url,

            'id_face' => $id_face_url,

            'id_back' => $id_back_url,

            'id_number' => $id_number,

            'car_business_time' => $this->date($car_business_time),

            'id_code' => $id_code,

            'test_time' => $this->date($test_time),

            'destory_time' => $this->date($destory_time),

        ];

        $ret = Db::name('car')->update($insert);

        if ($ret !== false) {

            return ['code' => 200, 'message' => '修改成功', 'car_face' => $car_face_url, 'car_back' => $car_back_url];

        }

        return ['code' => 400, 'message' => '失败'];



    }



    /**

     * @param $member_id

     * @return array

     * 车库列表

     */

    public function my_car_list($member_id)

    {

       $data = Db::name('car')->order('id','desc')->where('member_id',$member_id)->where('is_delete',0)->select();

       \think\Cache::set('car_'.$member_id,date('Y-m-d H:i:s'));



       return ['code' => 200, 'message' => '我的车库','data' => $data];

    }



    public function my_car_destory($member_id, $id)

    {

        $ret = Db::name('car')->where('member_id', $member_id)->where('id', $id)->delete();

        if ($ret) {

            return ['code' => '200', 'message' => '删除成功'];

        }

        return ['code' => '400', '失败'];

    }

    /**

     * 我的车修改

     */



    public function my_car_updates($member_id, $id, $field, $value)

    {

        $ret = Db::name('car')->where('id', $id)->where('member_id', $member_id)->setField($field, $value);

        if ($ret) {

            return ['code' => '200', 'message' => '修改成功'];

        }

        return ['code' => '400', '失败'];

    }



    /**

     * @param $member_id

     * @param $id

     * @param $field

     * @return array

     * 我的车图片修改

     */

    public function my_car_pic($member_id,$id,$field)

    {

        $value = uploadOne('update_pic');

        $ret = Db::name('car')->where('id',$id)->where('member_id',$member_id)->setField($field,$value);

        if($ret){

            return ['code' => '200','message' => '修改成功',$field => $value];

        }

        return ['code' => '400','失败'];

    }





    /**

     * @param $member_id

     * @return array

     * 会员卡

     */

    public function card($member_id)

    {

        $data = Db::name('member_card')->alias('mc')

            ->join('member m','mc.member_id = m.id')

            ->whereTime('end_time','>',time())

            ->whereTime('start_time','<',time())

            ->where('mc.member_id',$member_id)

            ->where('type','gt',0)

            ->fetchSql(false)

            ->order('id','desc')

            ->field('mc.order_id,mc.title,m.car_number,mc.id,mc.member_id,mc.content conetnt,mc.minus_count count,DATE_FORMAT(mc.start_time,"%Y.%m.%d") start_time,DATE_FORMAT(mc.end_time,"%Y.%m.%d") end_time,mc.type,mc.shop_id,mc.start_time create_time')

            ->select()->toArray();

        if(!empty($data)){

            foreach ($data as $k => &$v){

                $count = Db::name('order_mc_count')->where('order_id',$v['order_id'])->sum('gift_count');

                $v['count'] = $count;

            }

        }

        return ['code' => 200, 'message' => '会员卡', 'data' => $data];

    }



    /**

     * @param $member_id

     * @return array

     * 会员卡消费记录w

     */

    public function card_records($id)

    {

        $create_time = Db::name('member_card_records')

            ->alias('mcr')

            ->where('is_agree',2)

            ->where('card_id', $id)

            ->field('DATE_FORMAT(create_time,"%Y.%m.%d") create_time,shop_title,service_content,card_id,card_type')

            ->group('create_time')

            ->column('create_time');

        $data = Db::name('member_card_records')

            ->alias('mcr')

            ->where('is_agree',2)

            ->where('card_id', $id)

            ->where('create_time','in',$create_time)

            ->field('DATE_FORMAT(create_time,"%Y.%m.%d") create_time,shop_title,service_content,card_id,card_type')

            ->group('service_content')

            ->paginate(10)->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as $k => &$v){

                $v['service_content'] = Db::name('single')->where('id',$v['service_content'])->value('cat_name');

            }

        }

        return ['code' => 200, 'message' => '会员卡消费记录', 'data' => $data];

    }



//    public function card_records2($id)

//    {

//        $data = Db::name('member_card_records')

//            ->alias('mcr')

//            ->where('is_agree', 2)

//            ->where('card_id', $id)

//            ->field('create_time,shop_title,service_content,card_id,card_type')

//            ->group('create_time')

//            ->paginate(10)->toArray();

//        if(!empty($data['data'])){

//            foreach ($data['data'] as $k => &$v){

//

//            }8

//        }

//        halt($data);

//    }



    /**

     * @param $member_id

     * @return array

     * 我的礼包列表

     */

    public function my_gift($member_id)

    {

        $data = (new \app\index\model\Order())

            ->alias('o')

            ->join('gift g', 'o.gift_id = g.id','left')

            ->field('o.id order_id,gift_id,gift_title,gift_start_time,gift_end_time,o.day,g.single_pic')

            ->withField('order_gift_count')

            ->where("member_id = {$member_id} and o.status in (6,5,4) and o.t_type = 1 and gift_title != ''")

            ->order('o.id', 'desc')

            ->where('o.is_delete',1)

            ->where('o.auding_status',2)

            ->paginate(6)

            ->each(function ($item, $key) {

                $count_str = '';

                $order_gift_count = Db::name('order_gift_count ogc')

                    ->field('ogc.day,s.id,s.cat_name title,ogc.gift_origin_count zcs,ogc.gift_count sycs')

                    ->join('single s', 'ogc.single_content =  s.id')

                    ->where("order_id ={$item->order_id}")

                    ->select()->toArray();

                $max_time = Db::name('order_gift_count ogc')

                    ->field('ogc.day,s.id,s.cat_name title,ogc.gift_origin_count zcs,ogc.gift_count sycs')

                    ->join('single s', 'ogc.single_content =  s.id')

                    ->where("order_id ={$item->order_id}")

                    ->max('end_time');

                $day = Db::name('order_gift_count ogc')

                    ->field('ogc.day,s.id,s.cat_name title,ogc.gift_origin_count zcs,ogc.gift_count sycs')

                    ->join('single s', 'ogc.single_content =  s.id')

                    ->where("order_id ={$item->order_id}")

                    ->max('day');



                if (!empty($order_gift_count)) {

                    foreach ($order_gift_count as &$v) {

                        //$v['title'] = cut_str($v['title'],5);

                        $v['pinjie'] = $v['title'] . ':' . $v['zcs'] . '/' . $v['sycs'];

                        //$count_str .= $v['title'].':'.$v['zcs'].'/'.$v['sycs'].' ';

                        //$day += $v['day'];

                    }

                    $item->day = $day;

                    $item->gift_end_time = $max_time;

                }

                $item->gift_order_count = $order_gift_count;

                $shop_assignor_records = Db::name('shop_assignor_records')

                    ->alias('sar')

                    ->join('single s','sar.single_id = s.id','left')

                    ->join('shop sp','sar.shop_id = sp.id')

                    ->where('sar.order_id',$item->order_id)

                    ->where('is_agree',2)

                    ->field('sar.order_id,sar.assignor_content,DATE_FORMAT(sar.create_time,"%Y.%m.%d") create_time,sp.shop_title')

                    //s.cat_name assignor_type,

                    ->select()->toArray();//同意的礼包记录

                if(!empty($shop_assignor_records)){

                    foreach ($shop_assignor_records as &$v){

                        $v['assignor_content'] = Db::name('single')->where('id',$v['assignor_content'])->value('cat_name');

                    }

                }

                $item->records = $shop_assignor_records;



            });

        return ['code' => 200, 'message' => '购买成功的礼包列表', 'data' => $data];

    }



    /**

     * 当前订单礼包使用记录 1265

     */

    public function gift_use_records($order_id,$assignor_content)

    {

        $where = [];

        if(!empty($assignor_content_id)) $where['sar.assignor_content'] = ['eq',$assignor_content_id];

        $shop_assignor_records = Db::name('shop_assignor_records')

            ->alias('sar')

            ->join('single s', 'sar.single_id = s.id', 'left')

            ->join('shop sp', 'sar.shop_id = sp.id')

            ->where('sar.order_id', $order_id)

            ->where($where)

            ->where('is_agree', 2)

            ->field('sar.order_id,sar.assignor_content,DATE_FORMAT(sar.create_time,"%Y.%m.%d") create_time,sp.shop_title')

            //s.cat_name assignor_type,

            ->select()->toArray();//同意的礼包记录

        if (!empty($shop_assignor_records)) {

            foreach ($shop_assignor_records as &$v) {

                $v['assignor_content'] = Db::name('single')->where('id', $v['assignor_content'])->value('cat_name');

            }

        }

        return ['code' => 200, 'message' => '礼包记录', 'data' => $shop_assignor_records];

    }





    //https://www.aliyun.com/ss/6KGM6am26K-B6K-G5Yir

    //https://www.aliyun.com/ss/6am-6am26K-B6K-G5Yir/a

    public function read()

    {

        $host = "https://vehicle.market.alicloudapi.com";

        $path = "/vehicle";

        $method = "POST";

        $appcode = "8ac4b41d14ed415495c9662bbdaff312";

        $headers = array();

        array_push($headers, "Authorization:APPCODE " . $appcode);

        //根据API的要求，定义相对应的Content-Type

        array_push($headers, "Content-Type" . ":" . "application/x-www-form-urlencoded; charset=UTF-8");

        $querys = "";

        $bodys = "img=http://img3.fegine.com/image/vehicle.jpg";

        //或者base64

        //$bodys = "img=......";

        $url = $host . $path;

        $curl = curl_init();

        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);

        curl_setopt($curl, CURLOPT_URL, $url);

        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

        curl_setopt($curl, CURLOPT_FAILONERROR, false);

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        curl_setopt($curl, CURLOPT_HEADER, false);

        if (1 == strpos("$" . $host, "https://")) {

            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);

        }

        curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);

        $out_put = curl_exec($curl);

        echo $out_put;

    }



    public function axb($noa = 13703394745,$nob = 11161827755){



        $args = ['noa' => $noa,'nob' => $nob,'nox' => 17099527340,'expect_city' => '北京','expire_time' => 120];

        $axb_cache = Cache::get('axb'.$noa);



        $axb_cache_b = Cache::get('axb'.$nob);

        if(empty($axb_cache)){

            $ret = PlsDemo::bindAxb($args);

           // $ret = get_object_vars($ret);

            //halt($ret);

            Db::name('call_records')->insert(['noa' => $noa,'nob' => $nob,'SecretNo' =>$ret->SecretBindDTO->SecretNo,'Extension' =>$ret->SecretBindDTO->Extension,'SubsId' =>$ret->SecretBindDTO->SubsId]);

            Cache::set('axb'.$noa,$ret,120);

            Cache::set('axb'.$nob,$nob,120);

            return $ret;

        }else{

            if($nob != $axb_cache_b){ //两次好不一样

                return ['Code' => 'FAIL'];

            }

            return $axb_cache;

        }



    }







    /**

     * @param $member_id

     * 这个接口获取 也就是生成二维码 图片

     */

    public function get_car_qrcode($member_id)

    {

        $url = Request::instance()->domain().Url::build('index/member/show_web',['member_id' => $member_id]);

        $path =  ROOT_PATH . 'public/car_qrcode/' . $member_id . '.png';

        $logo = 'http://lihai.xmdonkey.com/logo.png';

        return $this->qrcodea($url,$path,$member_id,$logo);

    }



    /**

     * @param $member_id

     * @return array|mixed|\think\response\View

     * web页图片

     */

    public function show_web($member_id)

    {

        $data = db('member')->where('id', $member_id)->field('id,car_number,username')->find();

        if(empty($data['car_number'])){

            $data['car_number'] = '未认证';

        }

        $item = db('nuoche')->find();

        $item['multi_pic'] = unserialize($item['multi_pic']);

        $item['content'] = strip_tags($item['content']);



        return view('', ['data' => $data,'item' => $item]);

    }



    public function qrcodea($url,$path,$member_id,$logo)

    {

        Loader::import('phpqrcode.phpqrcode', EXTEND_PATH);

        $object = new \QRcode();

        //$path = ROOT_PATH . 'public/index/car_qrcode/' . $order_number . '.png';

        //第二个参数false的意思是不生成图片文件，如果你写上‘picture.png’则会在根目录下生成一个png格式的图片文件

        $ret = $object->png($url, $path, 10, 20, 2);

        $qrcode = imagecreatefromstring(file_get_contents($path));

        $logo = imagecreatefromstring(file_get_contents($logo));

        $qrcode_width = imagesx($qrcode);   //二维码图片宽度

        $qrcode_height = imagesy($qrcode);  //二维码图片高度

        $logo_width = imagesx($logo);       //logo图片宽度

        $logo_height = imagesy($logo);      //logo图片高度

        $logo_qr_width = $qrcode_width / 5;

        $scale = $logo_width / $logo_qr_width;

        $logo_qr_height = $logo_height / $scale;

        $from_width = ($qrcode_width - $logo_qr_width) / 2;

        //重新组合图片并调整大小[生成中间带logo的二维码  $qrcode]

        imagecopyresampled($qrcode, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);

        imagepng($qrcode, $path);



        $data = (new \app\index\model\Member())->where('id',$member_id)->field('id,username,avatar,nickname,car_number')->find();

        if(!empty($data['avatar'])){

            $data['avatar'] = Request::instance()->domain().'/'.$data['avatar'];

        }else{

            $data['avatar'] = '';

        }



        if(empty($data['nickname'])){

            $data['nickname'] = '未设置';

        }

        $data['image_url'] = Request::instance()->domain().'/car_qrcode/' . $member_id . '.png';

        return ['code' => 200 ,'message' => '二维码图片','data' =>$data];

        //exit;

    }



    public function bind_member_card($member_id = 1, $card_num = 2, $card_password = 3)

    {



        Db::startTrans();

        try {

            $is_certification = Db::name('member')->where('id',$member_id)->value('is_certification');

            if($is_certification == 1){

                //return ['code' => 401, 'message' => '请申请实名认证'];

            }

            if($is_certification == 3){

                //return ['code' => 402, 'message' => '实名认证审核中'];

            }

            $gift_card = Db::name('gift_card')->where('card_num',$card_num)->order('id desc')->find();//查询是否是礼包会原卡;

            $coupon_find = Db::name('coupon_card')->where('card_num', $card_num)->find();



            if (empty($coupon_find) && empty($gift_card)) {

                return ['code' => 400, 'message' => '会员卡不存在'];



            } elseif (!empty($gift_card)) {

                $check_password = Db::name('gift_card')->where('card_num', $card_num)->where('card_password', $card_password)->find();

                $gift_find = Db::name('gift')->where('is_delete',1)->where('id',$gift_card['gift_id'])->find();

                if(empty($gift_find)){

                    return ['code' => 400, 'message' => '礼包不存在'];

                }

                if (empty($check_password)) {

                    return ['code' => 400, 'message' => '礼包激活码不正确'];

                } else {



                    if($gift_find['auding_status'] != 2){

                        return ['code' => 400, 'message' => '礼包审核没有通过'];

                    }

                    if($gift_find['status'] != 1){

                        return ['code' => 400, 'message' => '会员卡没有开启'];

                    }



                    if(time() > strtotime($gift_find['create_time']) + $gift_find['jihuo_day']*3600*24){ //|| time() < strtotime($gift_find['start_time'])

                        return ['code' => 400, 'message' => '礼包激活时间失效'];

                    }



//                    dump(strtotime($gift_find['create_time']) + $gift_find['jihuo_day']*3600*24);

//                    halt(strtotime($gift_find['create_time']) + $gift_find['jihuo_day']*3600*24);

                    if ($check_password['status'] == 3) {

                        return ['code' => 400, 'message' => '当前会员卡禁止激活中'];

                    }

                    if ($check_password['status'] == 2) {

                        return ['code' => 400, 'message' => '会员卡已经激活'];

                    }

                    $args = ['member_id' => $member_id, 'id' => $gift_card['gift_id']];

                    $ret = \GuzzleHttp\json_decode(http_request('http://lihai.xmdonkey.com/index.php/index/gift_order/create', $args), true);

                    if ($ret['code'] == 200) {

                        Db::name('order')->where('order_number', $ret['order_number'])->update(['status' => 6, 'card_type' => 2,'card_num' => $card_num]);

                        $order_id = Db::name('order')->where('order_number', $ret['order_number'])->value('id');

                        Db::name('gift_card')->where('card_num', $card_num)->where('card_password', $card_password)->update(['status' => 2,'order_number' => $ret['order_number'],'jihuo_time' => date('Y-m-d H:i:s'),'order_id' => $order_id]);



                    }

                }





            }else{ //优惠券

                $check_password = Db::name('coupon_card')->where('card_num', $card_num)->where('card_password', $card_password)->find();



                $gift_find = Db::name('coupon')->where('id',$coupon_find['gift_id'])->find();

                if(empty($gift_find)){

                    return ['code' => 400, 'message' => '优惠券不存在'];

                }

                if (empty($check_password)) {

                    return ['code' => 400, 'message' => '优惠券激活码不正确'];

                } else {

                    if($gift_find['auding_status'] != 2){

                        return ['code' => 400, 'message' => '优惠券审核没有通过'];

                    }

                    if($gift_find['status'] != 1){

                        return ['code' => 400, 'message' => '会员卡没有开启'];

                    }

                    if(time() > $gift_find['create_time']+ $gift_find['jihuo_day']*3600*24){

                        return ['code' => 400, 'message' => '激活时间失效'];

                    }



                    if ($check_password['status'] == 3) {

                        return ['code' => 400, 'message' => '当前会员卡禁止激活中'];

                    }

                    if ($check_password['status'] == 2) {

                        return ['code' => 400, 'message' => '会员卡已经激活'];

                    }



                    $args = ['member_id' => $member_id, 'id' => $gift_card['gift_id']];

                    $d = [

                        'type' => $gift_find['type'],

                        'single_id' => $gift_find['single_id'],

                        'member_id'     => $member_id,

                        'coupon_id'     => $gift_find['id'],

                        'title'         => $gift_find['title'],

                        'minus_price'  => $gift_find['actual_price'],

                        'total_price' => $gift_find['virtual_price'],

                        'content'       => $gift_find['content'],

                        'start_time'    => $gift_find['start_time'],

                        'end_time'      => $gift_find['end_time'],

                        'status'        => '1',

                        'multi_pic' => '',

                        'card_type' => '2',

                        'auding_status' => 2,

                        'create_time'   => time()

                    ];

                    $insertGetId = Db::name('member_coupon')->insertGetId($d);

                    Db::name('coupon_card')->where('card_num', $card_num)->where('card_password', $card_password)->update(['status' => 2,'jihuo_time' => date('Y-m-d H:i:s'),'order_id' => $insertGetId,'member_id' =>$member_id]);

                }

            }

            Db::commit();

            return ['code' => 200, 'message' => '绑定成功'];

        } catch (Exception $e) {

            Db::rollback();

            return ['code' => 500, 'message' => $e->getMessage()];

        }

    }



    public function scan_dir($dir = '/www/wwwroot/public/', $filter = array())

    {



        $data = scan_dir($dir);

        halt(json_decode(json_decode($data),true));



    }



    public function scandirFolder($path){

        return json(scandirFolder($path));

    }



}